package com.wms.ymcc.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.wms.ymcc.constant.ValidationConstants;
import com.wms.ymcc.domain.MediaFile;
import com.wms.ymcc.service.IMediaFileService;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;

/**
 * @author : 阿盛哟
 * @description :消费消息
 * @createDate : 2025/6/5 15:31
 */
@Service
@RocketMQMessageListener(
        topic = ValidationConstants.ROCKETMQ_TOPIC_MEDIA,
        selectorExpression = ValidationConstants.ROCKETMQ_TAGS_MEDIA,
        consumerGroup = "service-media-consumer")
//实现RocketMQ的接口
public class ServiceConsumerImpl implements RocketMQListener<MessageExt> {
    @Autowired
    private IMediaFileService mediaFileService;

    @Override
    public void onMessage(MessageExt messageExt) {
        // 1. 获取到消息  MediaFile 在发消息的时候要封装成json字符串
        try {
            String jsonStrMediaFile = new String(messageExt.getBody(), "UTF-8");
            MediaFile mediaFile = JSONObject.parseObject(jsonStrMediaFile, MediaFile.class);

            // 调用推流方法
            mediaFileService.handleFile2m3u8(mediaFile);

        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
}